﻿#include <QFile>
#include <QMessageBox>
#include <QIcon>
#include <QPixmap>
#include <QDesktopServices>
#include <QUrl>
#include "CTaskExecQueryDlg.h"
#include "CHeader.h"
#include "CSqlOper.h"
#include "CRobTaskOper.h"
#include "CTaskDetailsDlg.h"



CTaskExecQueryDlg::CTaskExecQueryDlg(QString sSceneGuid, CRBaseData* pBasicData, QWidget *parent)
	: m_sSceneGuid(sSceneGuid), m_pBasicData(pBasicData), QDialog(parent)
{
	HWND hwnd = reinterpret_cast<HWND>(winId());
	setNativeTitleBar(hwnd, RGB(56, 63, 81));  // 深灰色
	ui.setupUi(this);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	setStyleSheetFile();
	initUI();
	initConnect();
	slotPushButtonQuery();
}

CTaskExecQueryDlg::~CTaskExecQueryDlg()
{}

void CTaskExecQueryDlg::setStyleSheetFile()//设置样式文件
{
	//设置对话框标题
	this->setWindowTitle(A2T("任务结果查询"));
	QString sQss = setUiStyle("styleSheet");
	QString sMyqss = styleSheet();
	QString sMerged = sQss + "\n" + sMyqss; // 后面的会覆盖前面的相同规则	
	setStyleSheet(sMerged);
}

void CTaskExecQueryDlg::initConnect() //初始化槽函数连接
{
	connect(ui.pushButtonQuery, &QPushButton::clicked, this, &CTaskExecQueryDlg::slotPushButtonQuery);
	connect(ui.pushButtonDelDay, &QPushButton::clicked, this, &CTaskExecQueryDlg::slotDelDay);
	connect(ui.pushButtonAddDay, &QPushButton::clicked, this, &CTaskExecQueryDlg::slotAddDay);
	connect(ui.pushButtonClose, &QPushButton::clicked, this, &CTaskExecQueryDlg::slotClose);
	connect(ui.widget, SIGNAL(sigShowPdf(QString)), this, SLOT(slotShowPdf(QString)));
	connect(ui.widget, SIGNAL(sigShowTask(QString, QString, QString)), this, SLOT(slotShowTask(QString, QString, QString)));

}

void CTaskExecQueryDlg::initUI()//初始化ui界面
{
	//设置两时间编辑栏
	QDateTime timeTo = QDateTime::currentDateTime();
	timeTo.setTime(QTime::fromString("23:59:59", "hh:mm:ss"));
	QDateTime timeFrom = QDateTime::currentDateTime();
	timeFrom.setTime(QTime::fromString("00:00:00", "hh:mm:ss"));
	ui.dateTimeEditBegin->setDateTime(timeFrom);
	ui.dateTimeEditEnd->setDateTime(timeTo);
	ui.dateTimeEditBegin->setCalendarPopup(true);
	ui.dateTimeEditEnd->setCalendarPopup(true);

	//任务名称
	{
		m_mapTaskName.insert("", "*");
		ui.comTaskName->addItem("*");
		CTaskInfo taskInfo;
		taskInfo.m_sGuid = m_sSceneGuid;
		taskInfo.m_sTemplateFixedID = "";
		taskInfo.m_nTaskType = -1;
		taskInfo.m_nPeriodType = -1;
		//执行Sql查询语句
		QVector<QSqlRecord> vecRecord;
		QString sErrMsg = "";
		selectTaskInfo(taskInfo, vecRecord, sErrMsg);
		for (int i = 0; i < vecRecord.size(); i++)
		{
			QString sTaskGuid = vecRecord[i].value("GUID").toString();
			QString sTaskName = vecRecord[i].value("TASK_NAME").toString();
			m_mapTaskName.insert(sTaskGuid, sTaskName);
			ui.comTaskName->addItem(sTaskName);
		}
	}
	//初始化tablewidget
	ui.widget->InitUi(0);
}

void CTaskExecQueryDlg::slotPushButtonQuery()//查询按钮槽函数
{
	//执行Sql查询语句
	CTaskExecInfo taskInfo;
	taskInfo.m_sGuid = m_sSceneGuid;
	taskInfo.m_sTaskGuid = m_mapTaskName.key(ui.comTaskName->currentText());
	taskInfo.m_sStartTime = ui.dateTimeEditBegin->dateTime().toString("yyyy/MM/dd hh:mm:ss");
	taskInfo.m_sEndTime = ui.dateTimeEditEnd->dateTime().toString("yyyy/MM/dd hh:mm:ss");
	QVector<QSqlRecord> vecRecord;
	QString sErrMsg = "";
	selectTaskExec(taskInfo, vecRecord, sErrMsg);
	QVector<QVector<QString>>vecStr;
	vecStr.clear();
	ui.lineEditAlarmCount->clear();
	ui.lineEditExecedCount->clear();
	ui.lineEditNoExecCount->clear();
	ui.lineEditTaskExecCount->clear();
	ui.lineEditOverdueCount->clear();
	ui.lineEditExecingCount->clear();
	ui.lineEditStopCount->clear();
	bool bChecked = false;
	if (ui.checkBox->isChecked())
	{
		bChecked = true;
	}
	int nExecCount = 0;//任务执行记录总数
	int nExecedCount = 0;//已执行任务数
	int nNoExecCount = 0;//未执行任务数
	int nAlarmCount = 0;//报警任务数
	int nExecingCount = 0;//执行中任务数
	int nOverdueCount = 0;//过期任务数
	int nStopCount = 0;//任务中止数
	int nNo = 0;
	for (int i = 0; i < vecRecord.size(); i++)
	{
		if (bChecked == true)
		{
			if (vecRecord[i].value("ALARM_ITEM_COUNT").toInt() == 0)
			{
				continue;
			}
		}
		nExecCount++;
		QVector<QString> vecRow;
		vecRow.push_back(QString::number(nNo));//序号
		vecRow.push_back(vecRecord[i].value("TASK_NAME").toString());//任务名称
		vecRow.push_back(vecRecord[i].value("START_TIME").toDateTime().toString("yyyy/MM/dd hh:mm:ss"));//执行时间
		vecRow.push_back(vecRecord[i].value("ITEM_COUNT").toString());//巡检项总数
		int nCount = vecRecord[i].value("EXEC_ITEM_COUNT").toInt() - vecRecord[i].value("ALARM_ITEM_COUNT").toInt();
		vecRow.push_back(QString::number(nCount));//正常项数
		vecRow.push_back(vecRecord[i].value("ALARM_ITEM_COUNT").toString());//异常项数
		vecRow.push_back(vecRecord[i].value("GUID").toString());//操作执行记录Guid
		vecRow.push_back(vecRecord[i].value("PDF_PATH_URL").toString());//pdf报表
		int nExecStatus = vecRecord[i].value("EXEC_STATUS").toInt();
		if (nExecStatus == 0)
		{
			vecRow.push_back(A2T("未执行"));//任务执行状态
			nNoExecCount++;
		}
		else if (nExecStatus == 1)
		{
			vecRow.push_back(A2T("已执行"));//任务执行状态
			nExecedCount++;
		}
		else if (nExecStatus == 2)
		{
			vecRow.push_back(A2T("执行中"));//任务执行状态
			nExecingCount++;
		}
		else if (nExecStatus == 3)
		{
			vecRow.push_back(A2T("任务中止"));//任务执行状态
			nStopCount++;

		}
		else if (nExecStatus == 4)
		{
			vecRow.push_back(A2T("过期"));//任务执行状态
			nOverdueCount++;
		}
		else
		{
			vecRow.push_back(A2T("/"));//任务执行状态
		}
		vecRow.push_back(vecRecord[i].value("TASK_EXEC_DSC").toString());//任务执行异常描述
		vecRow.push_back(vecRecord[i].value("TASK_ERR_DSC").toString());//任务执行结果描述
		vecRow.push_back(vecRecord[i].value("SYNTH_PDF_PATH_URL").toString());//综合报表Url地址
		vecRow.push_back(vecRecord[i].value("COLLECT_ALARM_COUNT").toString());//持续巡检项报警项数
		vecStr.push_back(vecRow);
		nNo++;

		if (vecRecord[i].value("ALARM_ITEM_COUNT").toInt() > 0 || vecRecord[i].value("COLLECT_ALARM_COUNT").toInt() > 0)
		{
			nAlarmCount++;
		}
	}
	//更新统计描述
	ui.lineEditAlarmCount->setText(QString::number(nAlarmCount));
	ui.lineEditExecedCount->setText(QString::number(nExecedCount));
	ui.lineEditNoExecCount->setText(QString::number(nNoExecCount));
	ui.lineEditTaskExecCount->setText(QString::number(nExecCount));
	ui.lineEditExecingCount->setText(QString::number(nExecingCount));
	ui.lineEditOverdueCount->setText(QString::number(nOverdueCount));
	ui.lineEditStopCount->setText(QString::number(nStopCount));

	ui.widget->InitData(0, vecStr);
	ui.widget->setSmallSceneType(m_pBasicData->m_nSmallSceneType);//设置小场景类型
}

void CTaskExecQueryDlg::slotShowPdf(QString sPdfPath)
{
	if (sPdfPath.size() == 0)
	{
		messageBox(0, A2T("没有查询到当前任务生成的pdf报表!"));
		return;
	}

	//==========用默认网页打开该pdf=========
	// 构造文件URL
	QUrl pdfFileUrl(sPdfPath);

	// 打开默认的Web浏览器
	if (!QDesktopServices::openUrl(pdfFileUrl))
	{
		messageBox(2, A2T("打开报表失败"));
		return;
	}
}

void CTaskExecQueryDlg::slotShowSyncPdf(QString sPdfPath)
{
	if (sPdfPath.size() == 0)
	{
		messageBox(0, A2T("没有查询到当前任务生成的pdf报表!"));
		return;
	}

	//==========用默认网页打开该pdf=========
	// 构造文件URL
	QUrl pdfFileUrl(sPdfPath);

	// 打开默认的Web浏览器
	if (!QDesktopServices::openUrl(pdfFileUrl))
	{
		messageBox(2, A2T("打开报表失败"));
		return;
	}
}

void CTaskExecQueryDlg::slotShowTask(QString sTaskExecGuid, QString sTaskName, QString sTaskExecDesc)
{
	CTaskDetailsDlg oper(m_sSceneGuid, sTaskExecGuid, sTaskName, m_pBasicData, sTaskExecDesc);
	oper.exec();
}

void CTaskExecQueryDlg::slotDelDay()
{
	QDateTime timeTo = ui.dateTimeEditBegin->dateTime().addDays(-1);
	timeTo.setTime(QTime::fromString("23:59:59", "hh:mm:ss"));
	QDateTime timeFrom = ui.dateTimeEditBegin->dateTime().addDays(-1);
	timeFrom.setTime(QTime::fromString("00:00:00", "hh:mm:ss"));
	ui.dateTimeEditBegin->setDateTime(timeFrom);
	ui.dateTimeEditEnd->setDateTime(timeTo);
	slotPushButtonQuery();
}

void CTaskExecQueryDlg::slotAddDay()
{
	QDateTime timeTo = ui.dateTimeEditBegin->dateTime().addDays(1);
	timeTo.setTime(QTime::fromString("23:59:59", "hh:mm:ss"));
	QDateTime timeFrom = ui.dateTimeEditBegin->dateTime().addDays(1);
	timeFrom.setTime(QTime::fromString("00:00:00", "hh:mm:ss"));
	ui.dateTimeEditBegin->setDateTime(timeFrom);
	ui.dateTimeEditEnd->setDateTime(timeTo);
	slotPushButtonQuery();
}

void CTaskExecQueryDlg::slotClose()
{
	this->close();
}
